﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Services;
using System.Collections;

namespace Catering.Catering_Page.system
{
    public partial class OperationArea : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.LoadData();
                this.LoadRep();
            }
        }

        //加载页面数据
        public void LoadData()
        {
            DataTable dt_OperationArea = new Catering_Rules.Company().GetAllList().Tables[0];
            this.Drop_Company.LoadDrop(dt_OperationArea, "id", "CName");
        }

        //加载Rep
        public void LoadRep()
        {
            DataTable dt_OperationArea = new Catering_Rules.OperationArea().GetList("",true).Tables[0];
            this.Rep_OperationArea.DataSource = dt_OperationArea;
            this.Rep_OperationArea.DataBind();
        }

        //获取
        [WebMethod]
        public static ArrayList Get(string cid)
        {
            ArrayList al = new ArrayList();
            Catering_Model.OperationArea md = new Catering_Rules.OperationArea().GetModel(Convert.ToInt32(cid));
            al.Add(md.AreaName);
            al.Add(md.AreaAdd);            
            al.Add(md.Back);
            al.Add(md.temp2);
            return al;
        }

        //添加
        [WebMethod]
        public static string Add(string AreaName, string AreaAdd, string Back, string Company)
        {
            Catering_Model.OperationArea md = new Catering_Model.OperationArea();
            md.id = new Catering_Rules.OperationArea().GetMaxId();
            md.AreaName = AreaName;
            md.AreaAdd = AreaAdd;           
            md.Back = Back;
            md.temp2 = Company;
            new Catering_Rules.OperationArea().Add(md);
            return "OK";
        }

        //修改
        [WebMethod]
        public static string Edit(string cid, string AreaName, string AreaAdd, string Back, string Company)
        {
            Catering_Model.OperationArea md = new Catering_Model.OperationArea();
            md.id = Convert.ToInt32(cid);
            md.AreaName = AreaName;
            md.AreaAdd = AreaAdd;
            md.Back = Back;
            md.temp2 = Company;
            new Catering_Rules.OperationArea().Update(md);
            return "OK";
        }

        //删除
        protected void Rep_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                int delID = Convert.ToInt32(e.CommandArgument);
                if (new Catering_Rules.OperationArea().Delete(delID))
                {
                    this.LoadRep();
                }
            }
            else if (e.CommandName == "tz")
            {
                int ID = Convert.ToInt32(e.CommandArgument);
                Catering_Model.OperationArea md = new Catering_Rules.OperationArea().GetModel(ID);
                if (md.temp1 == null || md.temp1.ToString() == "" || md.temp1.ToString() == "0")
                {
                    md.temp1 = "1";
                }
                else
                {
                    md.temp1 = "0";
                }

                if (new Catering_Rules.OperationArea().Update(md))
                {
                    this.LoadRep();
                }
            }
        }

        //获取公司名称
        public string GetCompanyName(string CompanyID)
        {
            if (CompanyID == "")
                return "";

            int uid = Convert.ToInt32(CompanyID);
            Catering_Model.Company md = new Catering_Rules.Company().GetModel(uid);
            return md != null ? md.CName : "";
        }
    }
}